<?php
final class autoload{

	public static function run(){
		//检查php版本
		self::getPhpVersion();
		//设置常量
		self::_set_const();
		defined('DEBUG') || define('DEBUG', false);
		if(DEBUG){
			//创建目录
			self::_create_dir();
			//引入文件
		    self::_import_file();
		}else{
			//屏蔽所有错误
			error_reporting(0);
			require TEMP_PATH.'/~boot.php';
		}
		//初始化应用
		Application::run();
	}

	/**
	 * [_set_const 设置常量]
	 */
	private static function _set_const(){
		//定义系统常量
		$path = str_replace("\\", "/", __FILE__);
		define('SYSTEM_PATH', dirname($path));
		define('SYSTEM_CONFIG_PATH', SYSTEM_PATH.'/Config');
		define('SYSTEM_DATA_PATH', SYSTEM_PATH.'/Data');
		define('SYSTEM_LIB_PATH', SYSTEM_PATH.'/Lib');
		define('SYSTEM_CORE_PATH', SYSTEM_LIB_PATH.'/Core');
		define('SYSTEM_FUNCTION_PATH', SYSTEM_LIB_PATH.'/Function');

		//定义框架扩展目录
		define('EXTENDS_PATH', SYSTEM_PATH.'/Extends');
		define('EXTENDS_ORG_PATH', EXTENDS_PATH.'/Org');
		define('EXTENDS_TOOL_PATH', EXTENDS_PATH.'/Tool');

		//定义项目目录常量
		define('ROOT_PATH', dirname(SYSTEM_PATH));
		define('APP_PATH', ROOT_PATH.'/'.APP_NAME);
		define('APP_CONFIG_PATH', APP_PATH.'/Config');
		define('APP_CONTROLLER_PATH', APP_PATH.'/Controller');
		define('APP_MODEL_PATH', APP_PATH.'/Model');
		define('APP_TPL_PATH', APP_PATH.'/Tpl');
		define('APP_PUBLIC_PATH', APP_TPL_PATH.'/Public');

		//定义临时目录
		define('TEMP_PATH', ROOT_PATH.'/Temp');
		//日志目录
		define('LOG_PATH', TEMP_PATH.'/Log');

		//定义编译目录
		define('APP_COMPILE_PATH',TEMP_PATH.'/'.APP_NAME.'/Compile');
		//定义缓存目录
		define('APP_CACHE_PATH',TEMP_PATH.'/'.APP_NAME.'/Cache');

		//定义isPost
		define('IS_POST', ($_SERVER['REQUEST_METHOD'] == 'POST') ? true: false);
		//定义isGET
		define('IS_GET', ($_SERVER['REQUEST_METHOD'] == 'GET') ? true: false);		
		//定义isAjax
		if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'){
			define('IS_AJAX',  true);
		}else{
			define('IS_AJAX',  false);
		}

		//创建公共常量
		define('COMMON_PATH',ROOT_PATH.'/Common');
		//公共配置项文件
		define('COMMON_CONFIG_PATH',COMMON_PATH.'/Config');
		//公共模型文件
		define('COMMON_MODEL_PATH',COMMON_PATH.'/Model');
		//公共库文件
		define('COMMON_LIB_PATH',COMMON_PATH.'/Lib');

		//定义demo模板文件的路径
		define('APP_INDEX_PATH',APP_TPL_PATH.'/Index');
	}

	/**
	 * 生成目录
	 * [_create_dir description]
	 * @return [type] [description]
	 */
	private static function _create_dir(){
		//定义路径
		$arr = array(
			APP_PATH,
			APP_CONFIG_PATH,
			APP_CONTROLLER_PATH,
			APP_MODEL_PATH,
			APP_TPL_PATH,
			APP_PUBLIC_PATH,
			TEMP_PATH,
			LOG_PATH,
			COMMON_PATH,
			COMMON_CONFIG_PATH,
			COMMON_MODEL_PATH,
			COMMON_LIB_PATH,
			APP_COMPILE_PATH,
			APP_CACHE_PATH,
			APP_INDEX_PATH,
		);
		foreach ($arr as $v) {
			//先判断目录是否存在，不存在创建目录并给定权限。递归创建。
			is_dir($v) || mkdir($v,0777,true);
		}

		file_exists(APP_TPL_PATH."/success.html") || copy(SYSTEM_DATA_PATH.'/Tpl/success.html',APP_TPL_PATH."/success.html");
		file_exists(APP_TPL_PATH."/error.html") || copy(SYSTEM_DATA_PATH.'/Tpl/error.html',APP_TPL_PATH."/error.html");
	}

	/**
	 * 载入框架核心文件
	 * [_import_file description]
	 * @return [type] [description]
	 */
	private static function _import_file(){
		$fileArr = array(
			SYSTEM_FUNCTION_PATH .'/function.php',
			SYSTEM_CORE_PATH .'/Log.class.php',
			EXTENDS_ORG_PATH .'/libs/Smarty.class.php',
			SYSTEM_CORE_PATH .'/SmartyView.class.php',
			SYSTEM_CORE_PATH .'/Controller.class.php',
			SYSTEM_CORE_PATH .'/Application.class.php',
		);

		$str = "";
		foreach ($fileArr as $v) {
			$str .= trim(substr(file_get_contents($v), 5,-2));
			require_once($v);
		}

		$str = "<?php\r\n" .$str;
		file_put_contents(TEMP_PATH."/~boot.php", $str) || die('access not allow');
	}

	/**
	 * [getPhpVersion 检查php版本]
	 * @return [type] [description]
	 */
	private static function getPhpVersion(){
		$msg = '';
		if (version_compare(phpversion(), '5.2.0', '<')) {
		    $msg .= 'Please upgrade to PHP5.2 above<br/>';
		}

		if (!function_exists('openssl_encrypt')) {
		    $msg .= 'Extension php_openssl is not open<br/>';
		}
		if ($msg) {
		    die("<h1 style='margin:20px;color:#535353;font:36px/1.5 Helvetica, Arial'>
		        <span style='font-size:150px;'>:(</span><br/>{$msg}</h1>");
		}
	}
}

autoload::run();
?>